﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

//Author  : Shantha Kumar .T
//Blog    : http://www.ktskumar.com
//Twitter : http://twitter.com/ktskumar
//Facebook: http://www.facebook.com


namespace SPConsole.UploadDocument
{
    class Program
    {
        static void Main(string[] args)
        {

             //Copy WebService Settings
             string webUrl = "http://localhost";
             WSCopy.Copy copyService = new WSCopy.Copy();
             copyService.Url = webUrl+"/_vti_bin/copy.asmx";

            //Pass the Current User Credentials to the WebService
             copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

            //Pass the Custom User Credential to the WebService
            //copyService.Credentials = System.Net.CredentialCache.NetworkCredential("UserName", "Password", "Domain");
 
            //Declare and initiates the Copy WebService members for uploading
             string sourceUrl = "E:\\Sample.doc";
             string[] destinationUrl = { "http://localhost/Shared Documents/Sample.doc" };
             WSCopy.CopyResult cResult1 = new WSCopy.CopyResult();
             WSCopy.CopyResult cResult2 = new WSCopy.CopyResult();
             WSCopy.CopyResult[] cResultArray = { cResult1, cResult2 };
             WSCopy.FieldInformation fFiledInfo = new WSCopy.FieldInformation();
             //fFiledInfo.DisplayName = "Title";
             //fFiledInfo.Type = WSCopy.FieldType.Text;
             //fFiledInfo.Value = "Sample Description";
             WSCopy.FieldInformation[] fFiledInfoArray = { fFiledInfo };
 
            //Reading the document contents in to stream
             FileStream strm = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read);
             byte[] fileContents = new Byte[strm.Length];
             byte[] r = new Byte[strm.Length];
             int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
             strm.Close();
 
            //Copy the document from SourceUrl to destinationUrl with metadatas
             uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray);

             if (copyresult == 0)
                 Console.WriteLine("Document uploaded successfully from " + sourceUrl + " to " + destinationUrl[0]);
             else
                 Console.WriteLine("Document gets failed on uploading..");

             Console.Write("Press any key to exit...");
             Console.Read();
 

        }
    }
}
